package com.cloudtasks;
import com.cloudtasks.R;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class ScanActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_list_typepage);
       
        // Read the bar code number using the open-source ZXing software
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        startActivityForResult(intent, 0);
		
    	//The following is the recommended way to do it, but it requires the
    	//Barcode Scanner app or access to the Android Market (disabled on AVD)
		//IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this);
		//integrator.initiateScan();
    }
    
    // Should get called after the scanning Intent is finished
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	if (requestCode == 0) {
    		if (resultCode == RESULT_OK) {
    		    String contents = intent.getStringExtra("SCAN_RESULT");
    		    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    		    // Handle successful scan here
    		    //String productName = convertUpcToName("016000275645");
    		} else if (resultCode == RESULT_CANCELED) {
    			// Handle failed scan here
    		}
    	}
    }
    
    // Convert a UPC code (contained in a string) to a product name using a Google API
    public static String convertUpcToName(String upcCodeNum) {
    	String key = "\"title\": ";
    	String productName = "";
		String urlBase = "https://www.googleapis.com/shopping/search/v1/public/products?key=AIzaSyC1rRq2FwiJ3HS4ToOxYhAXEYvxgEW5F6k&country=US&q=";
		String urlText = urlBase + upcCodeNum;
    	BufferedReader in;
		try {
			URL url = new URL(urlText);
			in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;

			// Read the query from Google's Search API for Shopping line-by-line
			while ((inputLine = in.readLine()) != null) {
				// Stop reading when key is found; extract the product name
				int keyIndex = inputLine.indexOf(key);
				if (keyIndex != -1) {
					productName = inputLine.substring(keyIndex + key.length());
					int startIndex = productName.indexOf("\"");
					int endIndex = productName.lastIndexOf("\"");
					productName = productName.substring(startIndex + 1, endIndex);
					break;
				}
			in.close();
			}
		} catch (MalformedURLException e) {
			// Auto-generated catch block
			// TODO Fill this in
		} catch (IOException e) {
			// Auto-generated catch block
			// TODO Fill this in
		}
		return productName;
	}
    
}


